<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
        "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <meta charset="utf-8">
    <title>物品记录管理</title>
    <link rel="shortcut icon" href="../fffff.ico"/>
    <link rel="stylesheet" href="../layui/css/layui.css"/>
    <link rel="stylesheet" href="../css/my.css"/>
</head>
<body>
<!--		<ul class="layui-nav layui-bg-green my" lay-filter="">-->
<!--			<li class="layui-nav-item my">添加记录</li>-->
<!--		</ul>-->


<!--<form class="layui-form " action="" method="post">-->
    <div class="infoform">
        <div class="layui-form-item ">
            <label class="layui-form-label">物品名</label>
            <div class="layui-input-inline">
                <input type="text" name="user" lay-verify="required" id="name" placeholder="请输入" autocomplete="off"
                       class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">数量</label>
            <div class="layui-input-inline">
                <input type="text" name="password" lay-verify="required" id="count" placeholder="请输入"
                       autocomplete="off" class="layui-input">
            </div>

        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">存放地点</label>
            <div class="layui-input-inline">
                <input type="text" name="check" lay-verify="required" id="place" placeholder="请输入" autocomplete="off"
                       class="layui-input">
            </div>

        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">简介</label>
            <div class="layui-input-inline">
                <input type="text" name="check" lay-verify="required" id="desc" placeholder="请输入" autocomplete="off"
                       class="layui-input">
            </div>

        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <a class="layui-btn" lay-submit lay-filter="formDemo" id="in">添加</a>
                <!--	<a href="/info" class="layui-btn" lay-submit>返回</a>-->
            </div>
        </div>
    </div>
<!--</form>-->


<script src="../layui/layui.all.js"></script>
<!--<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>-->
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script >
    $(function () {
        $("#in").click(function () {

            if ($("#name").val() == "" || $("#count").val() == "" || $("#place").val() == "" || $("#desc").val() == "") {
                layer.msg("四项都不能为空");
                return;
            }
            var formData = new FormData();
            formData.append('goodName', $("#name").val());
            formData.append('quantity', $("#count").val());
            formData.append('goodPlace', $("#place").val());
            formData.append('detail', $("#desc").val());
            if (getUrlParam("goodId")) {
                formData.append('goodId', getUrlParam("goodId"));
            }
            $.ajax({
                url: "/goods/insert",
                type: 'POST',
                data: formData,
                contentType: false,
                processData: false,
                cache: false,
                success: function (data) {
                    if (data.success) {
                        layer.msg(data.msg);

                        $("#name").val("");
                        $("#count").val("");
                        $("#place").val("");
                        $("#desc").val("");

                        var index = parent.layer.getFrameIndex(window.name);
                        setTimeout(function () {
                            parent.layer.close(index)
                            // $(".layui-laypage-btn").trigger("click");
                        }, 700);

                        // $("layui-layer-ico layui-layer-close layui-layer-close1").trigger("click");
                    } else {
                        layer.msg(data.msg);
                    }
                }
            });
        })

        function getInfo() {
            $.ajax({
                url: "/goods/info",
                type: 'POST',
                dataType: 'json',
                contentType: false,
                processData: false,
                cache: false,
                success: function (data) {
                    if (data.success) {
                        var html = "";
                        data.data.map(function (item, index) {
                            html += "<tr><td>" + item.goodName + "</td><td>" + item.quantity + "</td><td>" + item.goodPlace + "</td><td>" + item.createTime
                                + "</td><td>" + item.updateTime + "</td><td>" + item.detail + "</td><td>" +
                                "						<a class=\"layui-btn  layui-btn-sm\" onclick=\"update(" + item.goodId + ")\">修改</a>" +
                                "						<a class=\"layui-btn layui-btn-danger layui-btn-sm\" onclick=\"del(" + item.goodId + ")\">删除</a>" + "</td></tr>"
                            $(".tb").html(html);
                        })
                    } else {
                        layer.msg(data.msg);
                    }
                }
            })
        }

        function getUrlParam(name) {
            var reg = new RegExp("(^|\\?|&)" + name + "=([^&]*)(\\s|&|$)", "i");
            if (reg.test(window.location.href)) {
                return unescape(RegExp.$2.replace(/\+/g, " "))
            } else {
                return false;
            }
        }

        if (getUrlParam("goodId")) {
            $.ajax({
                url: "/goods/get/" + getUrlParam("goodId"),
                type: 'GET',
                contentType: false,
                processData: false,
                cache: false,
                success: function (data) {
                    if (data.success) {
                        $("#name").val(data.data.goodName);
                        $("#count").val(data.data.quantity);
                        $("#place").val(data.data.goodPlace);
                        $("#desc").val(data.data.detail);
                    } else {
                        layer.msg(data.msg);
                    }
                }
            });
        }

    })



</script>

</body>

</html>
